local ranliang = fk.CreateSkill{
  name = "steam__ranliang",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["steam__ranliang"] = "燃梁",
  [":steam__ranliang"] = [[锁定技，你若造成致命伤害，将为你的手牌数，攻击范围，出杀次数之一附加'<a href="ranliang_benghuai">崩坏</a>'，
  且你发动其时，连环角色亦需选择以脱离横置。]],
  ["#steam__ranliang"] = "燃梁：请选择一项附加“崩坏”",
  ["ranliang_handcard"] = "手牌数",
  ["ranliang_range"] = "攻击范围",
  ["ranliang_slash"] = "出杀次数",

  ["@@steam__ranliang_handcard"]="燃梁手牌",
  ["@@steam__ranliang_range"]="燃梁攻击范围",
  ["@@steam__ranliang_slash"]="燃梁出杀次数",
  ["ranliang_benghuai"]="崩坏",
  [":ranliang_benghuai"]="回合结束时，自身不是对应数值最小的角色，你选择：1.减1点体力上限；2.对应数值-1。",

}

-- 造成致命伤害时触发
ranliang:addEffect(fk.DamageCaused, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(ranliang.name) and
      data.damage >= (data.to.hp + data.to.shield) and not player:isKongcheng()
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    
    -- 选择附加"崩坏"的属性
    local choices={}
    if player:getMark("@@steam__ranliang_handcard") == 0 then
      table.insert(choices,"ranliang_handcard")
    end
    if player:getMark("@@steam__ranliang_range") == 0 then
      table.insert(choices,"ranliang_range")
    end
    if player:getMark("@@steam__ranliang_slash") == 0 then
      table.insert(choices,"ranliang_slash")
    end
    
    local choice = room:askToChoice(player, {choices=choices, skill_name=ranliang.name, prompt="#steam__ranliang"})
    
    -- 设置崩坏标记
    local mark_name = "@@steam__" .. choice
    room:setPlayerMark(player, mark_name, 1)
    
    -- 让连环角色选择脱离横置
  end,
})

Fk:loadTranslationTable{
  ["ranliang_loseMaxHp"]="减1点体力上限",
  ["ranliang_losehandcard"]="手牌数-1",
  ["ranliang_loseattackrange"]="攻击范围-1",
  ["ranliang_loseslash"]="出杀次数-1",
}

--杀的使用上限
getSlashNum=function(player)
  local card = Fk:cloneCard("slash")
  local skill = card.skill
  local n = skill:getMaxUseTime(player, Player.HistoryPhase, card, nil)
  return n
end
-- 结算崩坏效果
ranliang:addEffect(fk.EventPhaseStart, {
  anim_type = "negative",
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(ranliang.name) and player.phase == Player.Finish and
     (player:getMark("@@steam__ranliang_handcard") == 1 or player:getMark("@@steam__ranliang_range") == 1 
     or player:getMark("@@steam__ranliang_slash") == 1)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    
    if player:getMark("@@steam__ranliang_handcard") == 1 and 
    table.find(player.room.alive_players, function(p)
      return p:getHandcardNum() < player:getHandcardNum()
    end) then
      local tars=table.filter(player.room.alive_players, function(p)
        return p.chained
      end)
      table.insertIfNeed(tars,player)
      local choice = room:askToJointChoice(player, {
        choices = {"ranliang_loseMaxHp", "ranliang_losehandcard"},
        players = tars,
        skill_name = ranliang.name,
      })
      for p,ch in pairs(choice) do
        if ch == "ranliang_loseMaxHp" then
          room:changeMaxHp(p, -1)
        elseif ch == "ranliang_losehandcard" then
          local id = table.random(p:getCardIds("h"))
          room:throwCard({ id }, ranliang.name, p, player)
        end
        if p.chained and p~=player then
          room:setChainState(p, false)
        end
      end
    elseif player:getMark("@@steam__ranliang_range") == 1 and 
    table.find(player.room.alive_players, function(p)
      return p:getAttackRange() < player:getAttackRange()
    end) then
      local tars=table.filter(player.room.alive_players, function(p)
        return p.chained
      end)
      table.insertIfNeed(tars,player)
      local choice = room:askToJointChoice(player, {
        choices = {"ranliang_loseMaxHp", "ranliang_loseattackrange"},
        players = tars,
        skill_name = ranliang.name,
      })
      for p,ch in pairs(choice) do
        if ch == "ranliang_loseMaxHp" then
          room:changeMaxHp(p, -1)
        elseif ch == "ranliang_loseattackrange" then
          room:addPlayerMark(player,ranliang.name.."-attackrange")
        end
        if p.chained and p~=player then
          room:setChainState(p, false)
        end
      end
    elseif player:getMark("@@steam__ranliang_slash") == 1 and 
    table.find(player.room.alive_players, function(p)
      return getSlashNum(p) < getSlashNum(player)
    end) then
      local tars=table.filter(player.room.alive_players, function(p)
        return p.chained
      end)
      table.insertIfNeed(tars,player)
      local choice = room:askToJointChoice(player, {
        choices = {"ranliang_loseMaxHp", "ranliang_loseslash"},
        players = tars,
        skill_name = ranliang.name,
      })
      for p,ch in pairs(choice) do
        if ch == "ranliang_loseMaxHp" then
          room:changeMaxHp(p, -1)
        elseif ch == "ranliang_loseslash" then
          room:addPlayerMark(player,ranliang.name.."-slash")
        end
        if p.chained and p~=player then
          room:setChainState(p, false)
        end
      end

    end
  end,
})

ranliang:addEffect("atkrange", {
  correct_func = function(self, from, to)
    if from:getMark(ranliang.name.."-attackrange")>0 then
      local n=from:getMark(ranliang.name.."-attackrange")
      return -n
    end
    return 0
  end,
})


ranliang:addEffect("targetmod", {
  residue_func = function(self, player, skill, scope)
    if player:getMark(ranliang.name.."-slash") > 0 and skill.trueName == "slash_skill" and scope == Player.HistoryPhase then
      local n=player:getMark(ranliang.name.."-slash")
      return -n
    end
  end,
})

return ranliang 